/*
 * Copyright 2011-2012 Redia A/S
 * 
 * This file is part of Indhold+ for Android.
 *
 *  Redia Library App for Android is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Redia Library App for Android is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Redia Library App for Android.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.redia.digiform.model;

import java.util.HashSet;
import java.util.Set;
import java.util.Vector;

public class DigiformObject {

	private Set<DigiformRecord> audioBooks;
	private Set<DigiformRecord> moreBySameCreator;
	private Set<DigiformRecord> moreFromSameSeries;
	private Set<AdhlObject> adhl;
	private Set<DigiformRecord> creatorDescriptions;
	private Set<DigiformRecord> eBooks;
	private DigiformRecord record;
	private Set<DigiformRecord> reviews;
	
	private static Vector<DigiformObject> current = new Vector<DigiformObject>();
	
	public DigiformObject() {
		audioBooks = new HashSet<DigiformRecord>();
		moreBySameCreator = new HashSet<DigiformRecord>();
		moreFromSameSeries = new HashSet<DigiformRecord>();
		adhl = new HashSet<AdhlObject>();
		creatorDescriptions = new HashSet<DigiformRecord>();
		eBooks = new HashSet<DigiformRecord>();
		reviews = new HashSet<DigiformRecord>();
	}
	
	public void addAudioBook(DigiformRecord ab) {
		audioBooks.add(ab);
	}
	
	public void addEbook(DigiformRecord eb) {
		eBooks.add(eb);
	}
	
	public boolean hasEBooks() {
		return eBooks.size()>0;
	}
	
	public Set<DigiformRecord> geteBooks(){
		return eBooks;
	}
	
	public Set<DigiformRecord> getAudioBooks() {
		return audioBooks;
	}
	
	public boolean hasAudioBook() {
		return audioBooks.size()>0;
	}

	public void addMoreOfSameCreator(DigiformRecord mosc) {
		moreBySameCreator.add(mosc);
	}
	
	public boolean hasMoreOfSameCreator() {
		return moreBySameCreator.size()>0;
	}
	
	public Set<DigiformRecord> getMoreOfSameCreator() {
		return moreBySameCreator;
	}
	
	public void addMoreFromSameSeries(DigiformRecord mfss) {
		moreFromSameSeries.add(mfss);
	}
	
	public boolean hasMoreFromSameSeries() {
		return moreFromSameSeries.size()>0;
	}
	
	public Set<DigiformRecord> getMoreFromSameSeries() {
		return moreFromSameSeries;
	}


	public void addAdhl(AdhlObject adhlobj) {
		adhl.add(adhlobj);
	}
	
	public Set<AdhlObject> getAdhl() {
		return adhl;
	}
	
	public boolean hasAdhl() {
		return adhl.size()>0;
	}

	public void addCreatorDescription(DigiformRecord creatorDescriptionRecord) {
		creatorDescriptions.add(creatorDescriptionRecord);
	}
	
	public Set<DigiformRecord> getCreatorDescription() {
		return creatorDescriptions;
	}

	public void setRecord(DigiformRecord rec) {
		record = rec;
	}
	
	public DigiformRecord getRecord() {
		return record;
	}

	public void addReview(DigiformRecord reviewRecord) {
		reviews.add(reviewRecord);
	}
	/**
	 * Push a new DigiformObject on to the stack, making it the new active
	 * object.
	 * 
	 * @param current The new current DigiformObject
	 */
	public static void pushCurrent(DigiformObject current) {
		DigiformObject.current.addElement(current);
	}
	
	/** 
	 * Look at the current DigiformObject but don't remove it from the
	 * stack.
	 * 
	 * @return A reference to the topmost (active) DigiformObject
	 */
	public static DigiformObject lookCurrent() {
		if (DigiformObject.current.isEmpty()) {
			return null;
		} else {
			return DigiformObject.current.lastElement();
		}
	}
	
	/** 
	 * Remove the current DigiformObject from the back stack
	 * 
	 * @return A reference to the removed object
	 */
	public static DigiformObject popCurrent() {
		if (DigiformObject.current.isEmpty()) {
			return null;
		} else {
			return DigiformObject.current.remove(DigiformObject.current.size()-1); // Remove and return last object
		}
	}

	public boolean hasReviewUrl() {
		for (DigiformRecord review : reviews) {
			String url = review.getUri();
			if (url!=null && !url.isEmpty()) {
				return true;
			}
		}
		return false;
	}
	
	public boolean hasCreatorDescription() {
		return creatorDescriptions.size()>0;
	}
	
	public String toString() {
		String res = "[";
		res += "title=" + record.getTitle();
		res += " faustid=" + record.getFaustId();
		res += " samecreator=" + moreBySameCreator.size(); 
		res += " adhl=" + adhl.size();
		res += " sameseries=" + moreFromSameSeries.size();
		res += "]";

		return res;
	}

	public Set<DigiformRecord> getReviews() {
		return reviews;
	}

	public static boolean hasCurrent() {
		return !DigiformObject.current.isEmpty();
	}

	public static void dumpStack() {
		System.out.println("DigiformObject stack:");
		String dump = "";
		for (DigiformObject o : current) {
			dump = o + "\n" + dump;
//			System.out.println(o);
		}
		System.out.println("stack: "+dump);
		System.out.println("End of DigiformObject stack");
	}

	public static void clearStack() {
		DigiformObject.current.clear();
	}
	
}
